﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class ExamPaperCreate : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        protected int qtypeid = 0;
        protected int SubjectId = 0;
        protected int status = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterUser"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (Request.QueryString["qtypeid"] != null && Request.QueryString["qtypeid"] != string.Empty)
            {
                this.qtypeid = int.Parse(Request.QueryString["qtypeid"]);
            }
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }
            if (Request.QueryString["pageindex"] != null && Request.QueryString["pageindex"] != string.Empty)
            {
                this.PageIndex = int.Parse(Request.QueryString["pageindex"]);
            }
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty)
            {
                this.ExamId = int.Parse(Request.QueryString["id"]);
                if (this.ExamId > 0)
                {
                    status =   BAL.ExamPaper.GetExamPaperInfoStatus(this.ExamId);
                }
            }
            if (this.Request["SubjectId"] != null && this.Request["SubjectId"].ToString() != "")
            {
                this.SubjectId = int.Parse(this.Request["SubjectId"].ToString());
            }
            if (!IsPostBack)
            {
                BindingQuestionType();
                LoadList();
            }
            SetPostData();
            if (this.status != 0)
            {
                LinkButton1.Visible = false;
            }
        }
        public int ExamId
        {
            get
            {
                if (this.ViewState["ExamId"] != null)
                {
                    return int.Parse(this.ViewState["ExamId"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                this.ViewState["ExamId"] = value;
            }

        }

        public int PageIndex
        {
            get
            {
                if (this.ViewState["PageIndex"] != null)
                {
                    return int.Parse(this.ViewState["PageIndex"].ToString());
                }
                else
                {
                    return 1;
                }
            }
            set
            {
                this.ViewState["PageIndex"] = value;
            }
        
        }
        void BindingQuestionType()
        {
            IList<QuesTypeInfo> List = QuesType.GetQuesTypeInfos(0, int.MaxValue);
            this.drpQuestionType.DataTextField = "Name";
            this.drpQuestionType.DataValueField = "Id";
            this.drpQuestionType.DataSource = List;
            this.drpQuestionType.DataBind();
            this.drpQuestionType.Items.Insert(0, new ListItem("选择", ""));
           
        }
        void SetPostData()
        {
            if (!Page.IsPostBack)
            {
                if (this.qtypeid > 0)
                {
                    this.drpQuestionType.SelectedValue = qtypeid.ToString();
                }
            }
            else
            {
                if (this.drpQuestionType.SelectedValue != "")
                {
                    qtypeid = int.Parse(drpQuestionType.SelectedValue);
                }
                else
                {
                    qtypeid = 0;
                }
            }
        }
        protected void LoadList()
        {

            if (qtypeid > 0)
            {
                Pages.AllCount = BAL.Question.GetQuestionsCount(this.ExamId, "", "", qtypeid.ToString(), "", "", "", "", "", "");
            }
            else
            {
                Pages.AllCount = BAL.Question.GetQuestionsCount(this.ExamId, "", "", "", "", "", "", "", "", "");
            }
            Pages.PageSize = 20;
            Pages.PageName = "ExamPaperCreate.aspx?SubjectId=" + SubjectId.ToString()+ "&pageindex=" + this.PageIndex.ToString() + "&id=" + ExamId.ToString() + "&qtypeid=" + qtypeid.ToString() + "&page=";
            Pages.Compute();
            PageLabel.Text = Pages.GetPage();
            IList<QuestionInfo> userinfos = new List<QuestionInfo>();
            if (qtypeid > 0)
            {
                userinfos = BAL.Question.GetQuestions(this.ExamId, "", "", qtypeid.ToString(), "", "", "", "", "", "", Pages.First, Pages.Max);
            }
            else
            {
                userinfos = BAL.Question.GetQuestions(this.ExamId, "", "", "", "", "", "", "", "", "", Pages.First, Pages.Max); 
            }
            UserLists.DataSource = userinfos;
            UserLists.DataBind();
        }

        protected void tbOrder_DataBinding(object sender, EventArgs e)
        {
            TextBox lb = sender as TextBox;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "")
                    {
                        lb.Text = "0";
                    }
                    else
                    {
                        int  order=   Paper_Ques.GetQuesInfoOrderIdByExamPagerId(this.ExamId, int.Parse( lb.Text.Trim()));
                        lb.Text = order.ToString();
                    }
                }
                catch
                {
                    lb.Text = "0";
                }
            }
        }
        protected void lbSubject_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    SubjectInfo item = Subject.GetSubjectInfoById(int.Parse(lb.Text));
                    if (item != null && item.Id != 0)
                    {
                        lb.Text = item.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        protected void lbDiff_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    else
                    {
                        DifficInfo item = Diffic.GetDifficInfoById(int.Parse(lb.Text));
                        if (item != null && item.Id != 0)
                        {
                            lb.Text = item.Name;
                        }
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        protected void lbKnow_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    else
                    {

                        KnowledgeInfo item = Knowledge.GetKnowledgeInfoById(int.Parse(lb.Text));
                        if (item != null && item.Id != 0)
                        {
                            lb.Text = item.Keyword + "&nbsp;&nbsp;" + item.Name;
                        }
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }
        protected void lbQuestionType_DataBinding(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            if (lb != null)
            {
                try
                {
                    if (lb.Text == "" || lb.Text == "0")
                    {
                        lb.Text = "";
                    }
                    QuesTypeInfo item = QuesType.GetQuesTypeInfoById(int.Parse(lb.Text));
                    if (item != null && item.Id != 0)
                    {
                        lb.Text = item.Name;
                    }
                }
                catch
                {
                    lb.Text = "";
                }
            }
        }

        protected void lbBack_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("ExamPaper.aspx?page="+this.PageIndex.ToString());
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem item in this.UserLists.Items)
                {
                    HtmlInputCheckBox chk = item.FindControl("ckItem") as HtmlInputCheckBox;
                    if (chk != null && chk.Checked)
                    {
                        Paper_Ques.Del(ExamId, int.Parse(chk.Value));
                    }
                }
                if (this.drpQuestionType.SelectedValue != "")
                {
                    this.qtypeid = int.Parse(drpQuestionType.SelectedValue);
                }
                else
                {
                    this.qtypeid = 0;
                }
                LoadList();
            }
            catch
            {

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.drpQuestionType.SelectedValue != "")
            {
                this.qtypeid = int.Parse(drpQuestionType.SelectedValue);
            }
            else
            {
                this.qtypeid = 0;
            }
            LoadList();
        }

        protected void btnSaveOrder_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            if (lb != null)
            {
                ListViewItem item  =lb.Parent as ListViewItem;
                if (item != null)
                {
                    TextBox tb = item.FindControl("tbOrder") as TextBox;
                    if (tb != null && lb.CommandArgument != "")
                    {
                        try
                        {
                            Paper_Ques.SavePaper_QueInfoOrderId(this.ExamId, int.Parse(lb.CommandArgument),int.Parse(tb.Text.Trim()));
                        }
                        catch
                        { 
                        
                        }
                    }
                }
            }
            btnSearch_Click(sender, e);
        }
       
    }
}

